require('../css/commCss/normalize.css')
require('../css/commCss/reset.css')
require('../css/commCss/property.css')
require('../css/commCss/basic.css')

require('../libs/fonts/iconfont.css')

require('../css/register.less')

//引入axios插件
const axios = require("axios")

//引入轻提示
require('weui')
const weui = require('weui.js')




// 文档加载事件
document.addEventListener('DOMContentLoaded',function(){


    // 获取元素
  let user = document.querySelector("#user")//手机账号
  let pwd = document.querySelector("#pwd")//密码
  let registerBtn = document.querySelector("#registerBtn")
  let checking = document.querySelector("#checking")




  /**
   * 1.点击登录验证
   */
  registerBtn.addEventListener('click',function(){



    // 判断
    if(!/^1[3-9][0-9]{9}$/.test(user.value)){
        // 手机号
        checking.textContent = '手机账号格式错误（11位）';
        return;

    }else if(!/^[\w]{6,12}$/.test(pwd.value)){
        // 验证码
        checking.textContent = '请输入6-12以字母开头密码';
        return;

    }else{
        // 都输入正确//清空错误信息
        checking.textContent = '';

        //发送请求-注册账号
        axios.post('http://139.9.177.51:8099/users/login', {
          account: user.value,
          password: pwd.value
        }).then(function (r) {
          if (r.data.status === 0) {
            
            //将用户id存入本地存储
            localStorage.setItem('userId', r.data.data.user.userId)

            // console.log(r.data);
            //成功-先弹一个轻提示   再跳转页面-跳登录
            weui.toast("登录成功",{
              duration: 2000,
              callback: function(){location.href = './home.html'}
            })

          } else {
            //失败-先弹一个轻提示
            checking.textContent = '登录失败';
            
          };
        })

    }









  })


})